package com.ygk.bluepath.core.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ygk.bluepath.core.BActivity;
import com.ygk.bluepath.R;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import com.ygk.bluepath.bean.ModuleBean;
import com.ygk.bluepath.core.Presenter.MainActivityPresenter;

import utils.LogUtils;
import utils.ui.adapter.CommonAdapter;
import utils.ui.adapter.ViewHolder;


public class MainActivity extends BActivity implements OnItemClickListener,View.OnClickListener,MainActivityView{

	private Button bt_fresh;
	private ListView mlistView;
	private CommonAdapter<ModuleBean> mAdapter;
	private List<ModuleBean> mlist;

	private MainActivityPresenter mainActivityPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        init();
    }

	private void init() {
		// TODO Auto-generated method stub
		mainActivityPresenter=new MainActivityPresenter(this,this);
		mlist=new ArrayList<ModuleBean>();

		bt_fresh= (Button) findViewById(R.id.bt_fresh);
		bt_fresh.setOnClickListener(this);
		mlistView=(ListView) findViewById(R.id.lv_main);
		mAdapter=new CommonAdapter<ModuleBean>(mlist, app, R.layout.item_module) {
			
			@Override
			public void convert(ViewHolder vh, int position, ModuleBean t) {
				// TODO Auto-generated method stub
				vh.setText(R.id.tv_module_name, t.getModuleName());
			}
		};

		mlistView.setAdapter(mAdapter);
		mlistView.setOnItemClickListener(this);

		mainActivityPresenter.freshView();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		
		ModuleBean moduleBean=mlist.get(position);
		LogUtils.logToFile("运行"+moduleBean.getOptType(),true);
		switch (moduleBean.getOptType()) {
		case 0://默认
			
			break;
		case 1://普通activity的跳转
			try {
				sta(this, Class.forName(mlist.get(position).getModuleUri()));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("Main", "no this Activity:"+mlist.get(position).getModuleUri());
			}
			break;
		case 2://跳转到设置中某页面
			startActivity(new Intent(moduleBean.getModuleUri()));
			break;
		case 3://执行网络数据请求
			mainActivityPresenter.doPost( moduleBean.getModuleUri(), (HashMap<String, Object>)mlist.get(position).getModuleMainParm(), true, true, "", "");

			break;
		default:

			break;
		}
		
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.bt_fresh:
					freshView();
				break;
		}
	}


	@Override
	public void freshView() {
			mlist.clear();
			mlist.add(new ModuleBean("扫描二维码", "module.scanner.CaptureActivity",null,1));
			mlist.add(new ModuleBean("跳转GPS设置页面",  Settings.ACTION_LOCATION_SOURCE_SETTINGS,null,2));

			bMap.put("token", sp.getToken());
			bMap.put("car_id", "353");

			mlist.add(new ModuleBean("测试接口请求（预约接口）","car/reserve",bMap,3));
			mlist.add(new ModuleBean("标识点记录与监听", "com.ygk.bluepath.core.View.WatchingActivity",null,1));
			mlist.add(new ModuleBean("登录", "com.ygk.bluepath.member.View.LoginActivity",null,1));
			mlist.add(new ModuleBean("注册", "com.ygk.bluepath.member.View.RegisterActivity",null,1));

			bMap.put("point_id", "1");

			mlist.add(new ModuleBean("接口请求（上报标识点）","car/pointReport",bMap,3));

			mAdapter.notifyDataSetChanged();

	}
}
